/** 
 * @Title: FindWords.java 
 * @Package com.zizo.autohome.word.meng.dao 
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月14日 上午10:01:21 
 */
package com.zizo.autohome.word.meng.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zizo.util.ConnectionPool;
import com.zizo.util.Util;

/**
 * @ClassName: FindWords
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月14日 上午10:01:21
 * 
 */
public class FindWords implements Runnable {
	private static final Connection conn = ConnectionPool.getConnection("autohome_syn", "localhost");

	private int id;

	public FindWords(int id) {
		super();
		this.id = id;
	}

	/**
	 * @Title: getData
	 * @Description: TODO 句
	 * @return void
	 * @author Alexis
	 */
	public void getData() {
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT bbs_bt,bbs_nr,bbs_ftsj FROM qczj_bbs_c_" + id;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String bbs_bt = rs.getString("bbs_bt");
				String bbs_nr = rs.getString("bbs_nr");
				String bbs_ftsj = rs.getString("bbs_ftsj");
				String[] bts = bbs_bt.split("\\}\\{");
				String[] nrs = bbs_nr.split("\\}\\{");
				for (String bt : bts) {
					boolean isMatch = matcherWord(bt);
					if (isMatch) {
						insert(id, bt, bbs_ftsj);
					}
				}
				for (String nr : nrs) {
					boolean isMatch = matcherWord(nr);
					if (isMatch) {
						insert(id, nr, bbs_ftsj);
					}
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Title: getDataFromPra
	 * @Description: TODO 段落
	 * @return void
	 * @author Alexis
	 */
	public void getDataFromPra() {
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT bbs_id,bbs_nr,bbs_ftsj FROM qczj_bbs_c_" + id;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int bbs_id = rs.getInt("bbs_id");
				String bbs_nr = rs.getString("bbs_nr");
				String bbs_ftsj = rs.getString("bbs_ftsj");
				if (bbs_nr.contains("铸铁")) {
					insertPra(id, bbs_nr, bbs_ftsj, "铸铁", bbs_id);
				} else if (bbs_nr.contains("发动机材料")) {
					insertPra(id, bbs_nr, bbs_ftsj, "发动机材料", bbs_id);
				} else if (bbs_nr.contains("缸盖材料")) {
					insertPra(id, bbs_nr, bbs_ftsj, "缸盖材料", bbs_id);
				} else if (bbs_nr.contains("缸体材料")) {
					insertPra(id, bbs_nr, bbs_ftsj, "缸体材料", bbs_id);
				} else if (bbs_nr.contains("发动机") && bbs_nr.contains("铝")) {
					insertPra(id, bbs_nr, bbs_ftsj, "发动机材料", bbs_id);
				} else if (bbs_nr.contains("发动机") && bbs_nr.contains("铁")) {
					insertPra(id, bbs_nr, bbs_ftsj, "发动机材料", bbs_id);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public boolean matcherWord(String data) {
		String regex = "铸铁|发动机材料|缸盖材料|缸体材料|(发动机.*铝)|(发动机.*铁)|(铁.*发动机)|(铝.*发动机)";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(data);
		return m.find();
	}

	public Matcher getMatcher(String data) {
		String regex = "铸铁|发动机材料|缸盖材料|缸体材料|(发动机.*铝)|(发动机.*铁)|(铁.*发动机)|(铝.*发动机)";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(data);
		return m;
	}

	public void insert(int carid, String content, String time) {
		PreparedStatement ps = null;
		String sql = "INSERT INTO Projects_2016.find_words_para(carid,content,time) VALUES(?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, carid);
			ps.setString(2, content);
			ps.setTimestamp(3, (Timestamp) Util.getTimeHMS(time));
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void insertPra(int carid, String content, String time, String word, int bbsid) {
		PreparedStatement ps = null;
		String sql = "INSERT INTO Projects_2016.find_words_para(carid,content,time,word,bbsid) VALUES(?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, carid);
			ps.setString(2, content);
			ps.setTimestamp(3, (Timestamp) Util.getTimeHMS(time));
			ps.setString(4, word);
			ps.setInt(5, bbsid);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void run() {
		getDataFromPra();
		System.out.println(id);
	}

	public static void main(String[] args) {
		ExecutorService fix = Executors.newFixedThreadPool(5);
		int[] ids = { 2062, 3857, 982, 614 };
		for (int id : ids) {
			fix.execute(new FindWords(id));
		}
	}
}
